const chalk = require('chalk')
const {spawn, spawnSync} = require('child_process')
const fs = require('fs')

console.log('当前项目运行路径-->',process.env.PWD)

const child_1 = spawn('ls', ['-ls', '/'])
console.log(chalk.blue(child_1.connected))

/**
 * stdout, 返回二进制数据
 */
child_1.stdout.on('data', (data)=> {
  // data 为Buffer
  // console.log(chalk.red(data.toString()))

  fs.writeFileSync('./目录.txt', data)
})

child_1.on('close', code=> {
  console.log('进程结束, 退出码', code)
})

console.log(chalk.green('-demo02----------------'))
const gt = spawn('git', ['log', '--oneline', '--graph'])

gt.stdout.on('data', (data) => {
  console.log('数据类型--',Object.prototype.toString.call(data))
  console.log('通过 spawn 读取到 git 的log 日志 git log --oneline --graph')
  console.log(data.toString())
})

gt.on('close', code => {
  console.log('demo02--进程结束, 退出码 --', code)
})